package cn.bytecollege.user.filter;

import cn.bytecollege.common.constant.Resource;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebFilter(filterName = "UserRegisterFilter",servletNames = "UserRegisterController")
public class UserRegisterFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req =(HttpServletRequest) request;
        HttpSession session = req.getSession();
        String msg = null;
        String method = req.getMethod();
        //获取表单数据
        String username= request.getParameter("username");
        String password = request.getParameter("password");
        String phonenumber = request.getParameter("phonenumber");
        String headImg = request.getParameter("headImg");
        String nickname = request.getParameter("nickname");
        String gender = request.getParameter("gender");
        if (method.equalsIgnoreCase("post")){ Pattern usernamePattern = Pattern.compile("\\w{5,12}");
            Matcher usernameMatcher = usernamePattern.matcher(username);
            if(!usernameMatcher.matches()){
                msg = "用户名只能包含数字、字母、下划线，长度为5-12位";
                request.setAttribute("msg",msg);
                request.getRequestDispatcher("/user/userRegister.jsp")
                        .forward(request, response);
                return;
            }

            Matcher passwordMatcher = usernamePattern.matcher(password);
            if(!passwordMatcher.matches()){
                msg = "密码只能包含数字、字母、下划线，长度为5-12位";
                request.setAttribute("msg",msg);
                request.getRequestDispatcher("/user/userRegister.jsp")
                        .forward(request, response);
                return;
            }
            if(phonenumber==null||phonenumber.equals("")){
                msg = "请填写电话";
                request.setAttribute("msg",msg);
                request.getRequestDispatcher("/user/userRegister.jsp")
                        .forward(request, response);
                return;
            }
            if(nickname==null||nickname.equals("")){
                msg = "请填写昵称";
                request.setAttribute("msg",msg);
                request.getRequestDispatcher("/user/userRegister.jsp")
                        .forward(request, response);
                return;
            }
            if(headImg==null||headImg.equals("")){
                msg = "请上传头像";
                request.setAttribute("msg",msg);
                request.getRequestDispatcher("/user/userRegister.jsp")
                        .forward(request, response);
                return;
            }
            chain.doFilter(request,response);
            return;
        }
        chain.doFilter(request, response);
        }
    }

